package com.bdcom.comp;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.annotation.PostConstruct;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.model.AbstractBarrier;
import com.bdcom.model.AbstractDevice;
import com.bdcom.util.PlannerUtil;

/**
 * 属性值面板
 * @author hero
 */
@Component
public class PropertyPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTable barrierProsTable = null;
	
	private JTable devProsTable = null;
	
	private JScrollPane scroll = null;
	
	private DefaultTableModel barrierModel = null;
	
	private DefaultTableModel devModel = null;

	@Autowired
	private WHLabelPanel whLabelPanel;
	@Autowired
	private ScaleLabelPanel scaleLabelPanel;
	@Autowired
	private ZoomSlider zoomSlider;
	@Autowired
	private JSlider signSlider;
	
	private PropertyPanel() {

		setLayout(new BorderLayout());
	}
	
	@PostConstruct
	private void init(){

		initScalePanel();

		initPropertyPanel();
		
		initSignRangePanel();
		
		setEnabled(false);

	}
	
	private void initScalePanel() {
		// TODO Auto-generated method stub
		//添加比例尺滑动器
		JPanel panel = new JPanel(new BorderLayout());

		panel.setBorder(createBorder(PlannerUtil.getString("scale")));
		
		panel.add(scaleLabelPanel,BorderLayout.NORTH);

		panel.add(zoomSlider,BorderLayout.CENTER);
		
		panel.add(whLabelPanel, BorderLayout.SOUTH);
		
		add(panel, BorderLayout.NORTH);
	}

	private void initSignRangePanel() {
		// TODO Auto-generated method stub
		//添加一个用于设置显示最低信号强度的滑动器
		JPanel panel = new JPanel(new BorderLayout());

		panel.setBorder(createBorder(PlannerUtil.getString("sign range")));
		
		panel.add(signSlider);
		
		add(panel,BorderLayout.SOUTH);
	}

	private void initPropertyPanel() {
		// TODO Auto-generated method stub
			barrierModel = new DefaultTableModel(PlannerUtil.TAB_HEADER, 0){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				// TODO Auto-generated method stub
				return false;
			}
		};

		barrierProsTable = new JTable(barrierModel);

		devModel = new DefaultTableModel(PlannerUtil.TAB_HEADER, 0){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				// TODO Auto-generated method stub
				return false;
			}
		};

		devProsTable = new JTable(devModel);

		scroll = new JScrollPane(barrierProsTable);

		JPanel panel = new JPanel(new BorderLayout());
		
		panel.setBorder(createBorder(PlannerUtil.getString("property")));
		
		panel.add(scroll);

		add(panel, BorderLayout.CENTER);
	}
	
	//创建简单标题
	private Border createBorder(String title) {
		// TODO Auto-generated method stub
			TitledBorder titledBorder = new TitledBorder(new BevelBorder(
					BevelBorder.LOWERED, null, null, null, null), title,
					TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLUE);
			return titledBorder;
	}

	public void setProperties(Object selectedObject){
	
		if(selectedObject==null)
		{
			cleanModel(devModel);
			cleanModel(barrierModel);
		}
		else if(selectedObject instanceof AbstractDevice)
		{
			setProperties((AbstractDevice)selectedObject);
		}
		else if(selectedObject instanceof AbstractBarrier)
		{
			setProperties((AbstractBarrier)selectedObject);
		}
	} 
	
	public void setProperties(AbstractDevice dev) {
		
		//清空model
		cleanModel(devModel);
		
		devModel.addRow(new Object[]{PlannerUtil.getString("device name"),dev.getDeviceName()});
		devModel.addRow(new Object[]{PlannerUtil.getString("mac address"),dev.getMacAddress()});
		devModel.addRow(new Object[]{PlannerUtil.getString("device type"),dev.getDeviceType().getTypeName()});
		
		scroll.setViewportView(devProsTable);
		scroll.revalidate();
	}
	
	public void setProperties(AbstractBarrier barrier) {
		
		cleanModel(barrierModel);
		
		barrierModel.addRow(new Object[]{PlannerUtil.getString("texture"),barrier.getBarrierType().getBarrierName()});
		
		barrierModel.addRow(new Object[]{PlannerUtil.getString("attenuation"),barrier.getBarrierType().getAttenuation()});
		
		barrierModel.addRow(new Object[]{PlannerUtil.getString("color"),barrier.getBarrierType().getColor()});
		
		scroll.setViewportView(barrierProsTable);
		
		scroll.revalidate();

	}
	
	private void cleanModel(DefaultTableModel model) {
		// TODO Auto-generated method stub
		
		int count=model.getRowCount();
		
		for(int i=count;i>0;i--){
			
			model.removeRow(i-1);
			
		}
	}

	@Override
	public void setEnabled(boolean enabled) {
		// TODO Auto-generated method stub
		super.setEnabled(enabled);
		
		signSlider.setEnabled(enabled);
		
		zoomSlider.setEnabled(enabled);
		
		repaint();		
	}
}


